extends Sprite2D

@export var grid:TileMapLayer
var timeCount:float = 0.0
var timeLimit:float = 3.0
var target_path: Array[Vector2i]

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	timeCount += delta
	if timeCount > timeLimit:
		timeCount = 0.0
		var mouse_pos = get_global_mouse_position()
		var start_co = grid.local_to_map(global_position)
		var target_co = grid.local_to_map(mouse_pos)
		target_path = grid.astar.get_id_path(start_co, target_co)
		
	if target_path and not target_path.is_empty():
		var target_pos = grid.map_to_local(target_path[0])
		global_position = global_position.move_toward(target_pos, 100 * delta)
		
		if global_position == target_pos:
			target_path.remove_at(0)
